<template>
  <div class="ma-content-block lg:flex justify-between">
    <!-- CRUD 组件 -->
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item field="uuid" label="用户ID筛选">
            <a-input v-model="searchForm.uuid" placeholder="请输入用户ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="user_keyword" label="用户信息搜索">
            <a-input v-model="searchForm.user_keyword" placeholder="请输入用户手机号/昵称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="name" label="姓名搜索">
            <a-input v-model="searchForm.name" placeholder="请输入姓名" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="community_name" label="社区名称搜索">
            <a-input v-model="searchForm.community_name" placeholder="请输入社区名称" allow-clear />
          </a-form-item>
        </a-col>
        <!-- <a-col :sm="8" :xs="24">
          <a-form-item field="status" label="状态">
            <sa-select v-model="searchForm.status" dict="data_status" placeholder="请选择状态" />
          </a-form-item>
        </a-col> -->
        <a-col :sm="16" :xs="24">
          <a-form-item field="create_time" label="时间范围">
            <a-range-picker v-model="created_time" :showTime="true" style="width: 100%" @change="SearchTime" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <!-- 状态列 -->
       <template #id="{ record }">
        {{ record.uuid }}/{{ record.nickname }}
      </template>
      <template #name="{ record }">
        <div>姓名:{{ record.name||'--' }}</div>
        <div>手机号:{{ record.phone }}</div>
        <div>社区名称:{{ record.community_name }}</div>
      </template>
      <template #form_info="{ record }">
        <div>ID:{{ record.inviter_user?.id }}</div>
        <div>昵称:{{ record.inviter_user?.nickname }}</div>
      </template>
      <template #address="{ record }">
        {{ record.province }}{{ record.city }}{{ record.district }}{{ record.address }}
      </template>
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />

    <!-- 查看表单 -->
    <view-form ref="viewRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import ViewForm from './view.vue'
import api from '@/api/leader'

const crudRef = ref()
const editRef = ref()
const viewRef = ref()
const created_time = ref([])
// 搜索表单
const searchForm = ref({
  name: '',
  uuid: '',
  phone: '',
  user_keyword: '',
  community_name: '',
  room_id: '',
  created_at_start_time: '',
  created_at_end_time: '',
})

// 搜索时间
const SearchTime = (val) => {
  searchForm.value.created_at_start_time = val[0]
  searchForm.value.created_at_end_time = val[1]
}

// 修改状态
const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: false,
  },
  edit: {
    show: true,
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: false,
  },
  import: {
    show: true,
    url: '/core/post/import',
    templateUrl: '/core/post/downloadTemplate',
    auth: ['/core/post/import'],
  },
  export: { show: true, url: '/admin/community/communityList', auth: ['/admin/community/communityList'] },
})

// SaTable 列配置
const columns = reactive([
  { title: '用户ID/昵称', dataIndex: 'id', width:180 },
  { title: '姓名/手机/社区名称', dataIndex: 'name', width: 220 },
  { title: '代理信息', dataIndex: 'form_info', width: 180 },
  { title: '门头照片', dataIndex: 'image', type: 'image', width: 180 },
  { title: '用户数量', dataIndex:'user_total', width:120},
  { title: '累计充值', dataIndex: 'total_sum_price', width: 120 },
  { title: '累计提现', dataIndex: 'total_withdraw_price', width: 120 },
  { title: '观看人数', dataIndex: 'total_look_count', width: 120 },
  { title: '签到人数', dataIndex: 'total_sign_count', width: 120 },
  { title: '红包领取人数', dataIndex: 'total_red_count', width: 120 },
  { title: '红包领取金额', dataIndex: 'total_red_sum', width: 120 },
  { title: '发放红包金额', dataIndex: 'team_red', width: 120 },
  { title: '门店地址', dataIndex: 'address', width: 220 },
  // { title: '状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 120 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
